//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmBlackScholesOp.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Experimental/Finitedifferences/FdmLinearOpComposite.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::CFdmBlackScholesOp (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ localVol, Microsoft::FSharp::Core::FSharpOption<Double>^ illegalLocalVolOverwrite) : CFdmLinearOpComposite(CFdmBlackScholesOp::typeid)
{
    CFdmMesher^ _Cmesher;
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phFdmBlackScholesOp = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        bool _localVol = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (localVol) ? (bool)ValueHelper::Convert (localVol->Value) : false); //4
        QuantLib::Real _illegalLocalVolOverwrite = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (illegalLocalVolOverwrite) ? (QuantLib::Real)ValueHelper::Convert (illegalLocalVolOverwrite->Value) : -Null<QuantLib::Real>()); //4
        _ppFdmBlackScholesOp = new boost::shared_ptr<QuantLib::FdmBlackScholesOp> (new QuantLib::FdmBlackScholesOp ( _mesher,  _process,  _strike,  _localVol,  _illegalLocalVolOverwrite ));
        SetFdmLinearOpComposite (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBlackScholesOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::CFdmBlackScholesOp (boost::shared_ptr<QuantLib::FdmBlackScholesOp>& childNative, Object^ owner) : CFdmLinearOpComposite(CFdmBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesOp = NULL;
#endif
	_ppFdmBlackScholesOp = &childNative;
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBlackScholesOp));
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::CFdmBlackScholesOp (QuantLib::FdmBlackScholesOp& childNative, Object^ owner) : CFdmLinearOpComposite(CFdmBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesOp = NULL;
#endif
	_ppFdmBlackScholesOp = new boost::shared_ptr<QuantLib::FdmBlackScholesOp> (&childNative);
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBlackScholesOp));
    _FdmBlackScholesOpOwner = owner;
    _FdmLinearOpCompositeOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::CFdmBlackScholesOp (CFdmBlackScholesOp^ copy) : CFdmLinearOpComposite(CFdmBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmBlackScholesOp = new boost::shared_ptr<QuantLib::FdmBlackScholesOp> (copy->GetShared());
        _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBlackScholesOp));
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::CFdmBlackScholesOp (System::Type^ t) : CFdmLinearOpComposite(CFdmBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesOp = NULL;
#endif
	if (!t->IsSubclassOf(CFdmBlackScholesOp::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::CFdmBlackScholesOp (QuantLib::Handle<QuantLib::FdmBlackScholesOp>& childNative, Object^ owner)  : CFdmLinearOpComposite(CFdmBlackScholesOp::typeid)
{
	_phFdmBlackScholesOp = &childNative;
	_ppFdmBlackScholesOp = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBlackScholesOp));
    _FdmBlackScholesOpOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::CFdmBlackScholesOp (QuantLib::Handle<QuantLib::FdmBlackScholesOp> childNative)  : CFdmLinearOpComposite(CFdmBlackScholesOp::typeid)
{
	_phFdmBlackScholesOp = &childNative;
	_ppFdmBlackScholesOp = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBlackScholesOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::CFdmBlackScholesOp (QuantLib::FdmBlackScholesOp childNative)  : CFdmLinearOpComposite(CFdmBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdmBlackScholesOp = NULL;
#endif
	_ppFdmBlackScholesOp = new boost::shared_ptr<QuantLib::FdmBlackScholesOp> (new QuantLib::FdmBlackScholesOp (childNative));
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBlackScholesOp));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::~CFdmBlackScholesOp ()
{
    if (_ppFdmBlackScholesOp != NULL)
    {
	    delete _ppFdmBlackScholesOp;
        _ppFdmBlackScholesOp = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::!CFdmBlackScholesOp ()
{
    if (_ppFdmBlackScholesOp != NULL)
    {
	    delete _ppFdmBlackScholesOp;
    }
}
QuantLib::FdmBlackScholesOp& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::GetReference ()
{
    if (_ppFdmBlackScholesOp == NULL) throw gcnew NativeNullException ();
	return **_ppFdmBlackScholesOp;
}
boost::shared_ptr<QuantLib::FdmBlackScholesOp>& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::GetShared ()
{
    if (_ppFdmBlackScholesOp == NULL) throw gcnew NativeNullException ();
	return *_ppFdmBlackScholesOp;
}
QuantLib::FdmBlackScholesOp* Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::GetPointer ()
{
    if (_ppFdmBlackScholesOp == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmBlackScholesOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmBlackScholesOp>& Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::GetHandle ()
{
	if (_phFdmBlackScholesOp == NULL)
	{
		_phFdmBlackScholesOp = new Handle<QuantLib::FdmBlackScholesOp> (*_ppFdmBlackScholesOp);
	}
	return *_phFdmBlackScholesOp;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::HasNative () 
{
	return (_ppFdmBlackScholesOp != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdmBlackScholesOp^ Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1);
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2);
    	(*_ppFdmBlackScholesOp)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmBlackScholesOp)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmBlackScholesOp^ Cephei::QL::Experimental::Finitedifferences::CFdmBlackScholesOp_Factory::Create (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ localVol, Microsoft::FSharp::Core::FSharpOption<Double>^ illegalLocalVolOverwrite)
{
    return gcnew CFdmBlackScholesOp ( mesher,  process,  strike,  localVol,  illegalLocalVolOverwrite);
}
